var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var ad = require("./ad");
var in_task = require("./inTask");
var point_map = require("./pointMap");
var gold_ad = {
  is_over_today: false,
  go_page_from_home() {
    var logger = utils.create_logger("gold_ad go_page_from_home: ");
    if (ocr_utils.screen_exists_text_all(["视频", "小说"], true)) {
      logger("SUCCESS");
      utils.click_point.apply(
        utils,
        utils.get_size_point(point_map["首页任务"])
      );
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      if (this.is_over_today) {
        logger("今天的 gold_ad 已结束");
        return;
      }
      this.init();
    } else {
      logger("NOT FOUND");
    }
  },
  init() {
    in_task.init(null, null, null, null, null, null, 3);
    var logger = utils.create_logger("gold_ad after_in_task: ");
    if (utils.click_text_starts_with("金币收益")) {
      logger("找到--金币收益--");
      sleep(2 * 1000);
      this.run();
    } else {
      logger("没找到--金币收益--");
    }
  },
  run() {
    var logger = utils.create_logger("gold_ad run: ");
    if (utils.exists_text_starts_with("看文章赚金币")) {
      logger("找到--看文章赚金币--");
      this.is_over_today = true;
      this.after();
      return;
    }
    var ui_text = utils.exists_text_matches(/看视频赚(\d)+金币/);
    if (ui_text) {
      logger("找到--看视频赚d+金币--");
      var gold = utils.get_number(ui_text.text());
      logger("--" + gold + "--金币");
      utils.click_text_matches(/看视频赚(\d)+金币/);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      ad.has_watch_ad_times = 0;
      ad.max_ad_times = 1000;
      ad.init(
        function () {
          var over_contents = ["已领取", "已完成任务"];
          for (var i = 0; i < over_contents.length; i++) {
            var over_content = over_contents[i];
            if (utils.exists_text_starts_with(over_content)) {
              logger("找到--" + over_content + "--");
              return true;
            }
          }
          return false;
        },
        function () {
          if (utils.exists_text_matches(/看视频赚(\d)+金币/)) {
            logger("找到--/看视频赚(d)+金币/--");
            return true;
          }
          if (utils.exists_text_starts_with("看文章赚金币")) {
            logger("找到--看文章赚金币--");
            return true;
          }
          return false;
        },
        function () {
          return ocr_utils.screen_exists_text_all(["视频", "小说"], false);
        },
        ["再看一个领取", "再看一条领取", "再看1个获得", "换个视频"],
        5
      );
      // if (ad.in_task) {
      //   this.run();
      //   return;
      // }
      this.after();
    } else {
      logger("没找到--/看视频赚(d)+金币/--");
      this.after();
    }
  },
  after() {
    console.log("gold_ad after: ");
    back();
    sleep(2 * 1000);
    this.next();
  },
  next() {
    console.log("gold_ad next: ");
  },
};

module.exports = gold_ad;
